﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Text;
using BO.Infrastructure.Utils;
using ChannelVN.Cached;

namespace BO.Photo
{
    public class PhotoBO : IPhotoBO
    {
        private readonly static PhotoBO_NoCache NoCacheLayer = new PhotoBO_NoCache();

        private static PhotoBO _ownerInstance;
        public static IPhotoBO GetInstance()
        {
            return _ownerInstance ?? (_ownerInstance = new PhotoBO());
        }

        static readonly string UniqueText = typeof(PhotoBO).Namespace + typeof(PhotoBO).Name;

        static string BuildCacheKey(string methodName, IEnumerable<object> listParams)
        {
            return PresentationUtils.GetCacheKey(methodName, listParams, UniqueText);
        }


        public DataTable GetPhotosInNews(long newsId, int top)
        {
            var cacheName = BuildCacheKey("GetPhotosInNews", new object[] { newsId , top});
            var result = CacheController.GetMemcachedInstanceForDataCached().Get<DataTable>(CacheController.CacheGroupForDataCached, cacheName);
            if (result == null)
            {
                result = NoCacheLayer.GetPhotosInNews(newsId, top);
                if (result != null)
                {
                    CacheController.GetMemcachedInstanceForDataCached().Add(CacheController.CacheGroupForDataCached, cacheName, result, DateTime.Now.AddMinutes(3));
                }
            }
            return result;
        }
    }
}
